﻿using DotNetUI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DotNetUI.Controls
{

    /// <summary>
    /// 数码管
    /// </summary>
    public class DotNetDigitalItem : Control, IDotNetDigitalSegmentItem
    {
        /// <summary>
        /// 已映射的字符
        /// </summary>
        public Dictionary<char, IDotNetDigitalSegmentItem> CharMaped { get; set; } = new Dictionary<char, IDotNetDigitalSegmentItem>()
        {
            {'1',new DotNetDigitalSegmentItem{BSegment=true,DSegment=true } },
            {'2',new DotNetDigitalSegmentItem{ASegment=true,CSegment=true,BSegment=true,ESegment=true,FSegment=true } },
            {'3',new DotNetDigitalSegmentItem{ ASegment=true,BSegment=true,CSegment=true,DSegment=true,ESegment=true} },
            {'4',new DotNetDigitalSegmentItem{BSegment=true,CSegment=true,DSegment=true ,GSegment=true} },
            {'5',new DotNetDigitalSegmentItem{ ASegment=true,CSegment=true,DSegment=true,ESegment=true,GSegment=true} },
            {'6',new DotNetDigitalSegmentItem{BSegment=false,ASegment=true,CSegment=true,DSegment=true,ESegment=true,FSegment=true,GSegment=true } },
            {'7',new DotNetDigitalSegmentItem{ASegment=true,BSegment=true,DSegment=true } },
            {'8',new DotNetDigitalSegmentItem{ASegment=true,BSegment=true,CSegment=true,DSegment=true,ESegment=true,FSegment=true,GSegment=true   } },
            {'9',new DotNetDigitalSegmentItem{ ASegment=true,BSegment=true,CSegment=true,DSegment=true,ESegment=true,FSegment=false,GSegment=true} },
            {'0',new DotNetDigitalSegmentItem{ASegment=true,BSegment=true,CSegment=false,DSegment=true,ESegment=true,FSegment=true,GSegment=true } },
            {'A',new DotNetDigitalSegmentItem{ASegment=true,BSegment=true,CSegment=true,DSegment=true,FSegment=true,GSegment=true } },
            {'B',new DotNetDigitalSegmentItem{CSegment=true,DSegment=true,ESegment=true,FSegment=true,GSegment=true } },
            {'C',new DotNetDigitalSegmentItem{ASegment=true,ESegment=true,FSegment=true,GSegment=true } },
            {'D',new DotNetDigitalSegmentItem{BSegment=true,CSegment=true,DSegment=true,ESegment=true,FSegment=true, } },
            {'E',new DotNetDigitalSegmentItem{ASegment=true,CSegment=true,ESegment=true,FSegment=true,GSegment=true } },
            {'F',new DotNetDigitalSegmentItem{ASegment=true,CSegment=true,FSegment=true,GSegment=true } },
            {'G',new DotNetDigitalSegmentItem{ ASegment=true,BSegment=true,CSegment=true,DSegment=true,ESegment=true,GSegment=true} },
            {'H',new DotNetDigitalSegmentItem{BSegment=true,CSegment=true,DSegment=true,FSegment=true,GSegment=true } },
            {'h',new DotNetDigitalSegmentItem{BSegment=true,CSegment=true,DSegment=true,FSegment=true,GSegment=true } },
            {'I',new DotNetDigitalSegmentItem{BSegment=true,DSegment=true } },
            {'J',new DotNetDigitalSegmentItem{BSegment=true,DSegment=true,ESegment=true   } },
            {'l',new DotNetDigitalSegmentItem{ASegment=true,BSegment=true,CSegment=true,DSegment=true,ESegment=true,FSegment=true,GSegment=true   } },
            {'L',new DotNetDigitalSegmentItem{ESegment=true,FSegment=true,GSegment=true   } },
            {'n',new DotNetDigitalSegmentItem{FSegment=true ,CSegment=true,DSegment=true   } },
            {'N',new DotNetDigitalSegmentItem{FSegment=true ,CSegment=true,DSegment=true  } },
            {'P',new DotNetDigitalSegmentItem{ASegment=true,BSegment=true,CSegment=true,FSegment=true,GSegment=true  } },
            {'O',new DotNetDigitalSegmentItem{ASegment=true,BSegment=true,CSegment=false,DSegment=true,ESegment=true,FSegment=true,GSegment=true } },
            {'S',new DotNetDigitalSegmentItem{ASegment=true,CSegment=true,DSegment=true,ESegment=true,GSegment=true  } },
            {'T',new DotNetDigitalSegmentItem{ASegment=true,FSegment=true,GSegment=true  } },
            {'U',new DotNetDigitalSegmentItem{BSegment=true,DSegment=true,ESegment=true,FSegment=true,GSegment=true  } },
            {'V',new DotNetDigitalSegmentItem{DSegment=true,ESegment=true,FSegment=true,} },
            {'R',new DotNetDigitalSegmentItem{CSegment=true,FSegment=true,} },
            {'-',new DotNetDigitalSegmentItem{CSegment=true,} },
        };
        static DotNetDigitalItem()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(DotNetDigitalItem), new FrameworkPropertyMetadata(typeof(DotNetDigitalItem)));
        }
        /// <summary>
        /// 字符属性
        /// </summary>
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register(nameof(Text), typeof(char), typeof(DotNetDigitalItem), new PropertyMetadata(' ', new PropertyChangedCallback((s, e) =>
          {
              if (s is DotNetDigitalItem item)
              {
                  item.OnTextChange((char)e.NewValue);
              }
          })));
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            var canvans=(Canvas) this.Template.FindName("_Canvas", this);
            if (canvans!=null)
            {
                canvans.PreviewMouseDown += Canvans_MouseDown;
            }
        }

        private void Canvans_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (sender is Canvas canvas)
            {
                if (Mouse.DirectlyOver is Path path)
                {
                    var name = path.Name + "Segment";
                    var property= this.GetType().GetProperty(name);

                    if (path.Fill?.ToString()!=this.HightlightBrush.ToString())
                    {
                       // path.Fill = this.HightlightBrush;
                        property.SetValue(this, true);
                    }
                    else
                    {
                        property.SetValue(this, false);
                        //path.Fill = new SolidColorBrush(Colors.White);
                    }
                }
            }
        }

        /// <summary>
        /// 当腰获取映射时触发
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        protected IDotNetDigitalSegmentItem OnGetMap(char value)
        {
            value = Char.ToUpper(value);
            if (!CharMaped.ContainsKey(value))
            {
                return null;
            }
            var data = CharMaped[value];
            return data;
        }
        /// <summary>
        /// 当字符被修改时触发
        /// </summary>
        /// <param name="value"></param>
        protected void OnTextChange(char value)
        {
            var data = OnGetMap(value);
            if (data == null)
            {
                return;
            }
            this.ASegment = data.ASegment;
            this.BSegment = data.BSegment;
            this.CSegment = data.CSegment;
            this.DSegment = data.DSegment;
            this.ESegment = data.ESegment;
            this.FSegment = data.FSegment;
            this.GSegment = data.GSegment;
        }
        /// <summary>
        /// 显示的字符
        /// </summary>
        public char Text { get => this.GetValue<char>(TextProperty); set => SetValue(TextProperty, value); }
        /// <summary>
        /// 高亮笔刷
        /// </summary>
        public Brush HightlightBrush { get => this.GetValue<Brush>(HightlightBrushProperty); set => SetValue(HightlightBrushProperty, value); }
        /// <summary>
        /// 高亮笔刷属性
        /// </summary>

        public static readonly DependencyProperty HightlightBrushProperty = DependencyProperty.Register(nameof(HightlightBrush), typeof(Brush), typeof(DotNetDigitalItem));
        /// <summary>
        /// A片段属性
        /// </summary>
        public static readonly DependencyProperty ASegmentProperty = DependencyProperty.Register(nameof(ASegment), typeof(bool), typeof(DotNetDigitalItem));
        /// <summary>
        /// B
        /// </summary>
        public static readonly DependencyProperty BSegmentProperty = DependencyProperty.Register(nameof(BSegment), typeof(bool), typeof(DotNetDigitalItem));

        /// <summary>
        /// C
        /// </summary>
        public static readonly DependencyProperty CSegmentProperty = DependencyProperty.Register(nameof(CSegment), typeof(bool), typeof(DotNetDigitalItem));
        /// <summary>
        /// D
        /// </summary>
        public static readonly DependencyProperty DSegmentProperty = DependencyProperty.Register(nameof(DSegment), typeof(bool), typeof(DotNetDigitalItem));
        /// <summary>
        /// E
        /// </summary>
        public static readonly DependencyProperty ESegmentProperty = DependencyProperty.Register(nameof(ESegment), typeof(bool), typeof(DotNetDigitalItem));
        /// <summary>
        /// F
        /// </summary>
        public static readonly DependencyProperty FSegmentProperty = DependencyProperty.Register(nameof(FSegment), typeof(bool), typeof(DotNetDigitalItem));
        /// <summary>
        /// G
        /// </summary>
        public static readonly DependencyProperty GSegmentProperty = DependencyProperty.Register(nameof(GSegment), typeof(bool), typeof(DotNetDigitalItem));


        /// <summary>
        /// 边框粗细
        /// </summary>
        public static readonly DependencyProperty SegmentBorderThicknessProperty = DependencyProperty.Register(nameof(SegmentBorderThickness), typeof(double), typeof(DotNetDigitalItem));
        /// <summary>
        /// 片段粗细
        /// </summary>
        public double SegmentBorderThickness { get => this.GetValue<double>(SegmentBorderThicknessProperty); set => SetValue(SegmentBorderThicknessProperty, value); }

        /// <summary>
        /// 片段边框颜色
        /// </summary>
        public static readonly DependencyProperty SegmentBorderBrushProperty = DependencyProperty.Register(nameof(SegmentBorderBrush), typeof(Brush), typeof(DotNetDigitalItem));
        /// <summary>
        /// 片段边框颜色
        /// </summary>
        public Brush SegmentBorderBrush { get => this.GetValue<Brush>(SegmentBorderBrushProperty); set => SetValue(SegmentBorderBrushProperty, value); }
        /// <summary>
        /// A
        /// </summary>
        public bool ASegment { get => this.GetValue<bool>(ASegmentProperty); set => SetValue(ASegmentProperty, value); }
        /// <summary>                                                                   
        /// B
        /// </summary>                                                          
        public bool BSegment { get => this.GetValue<bool>(BSegmentProperty); set => SetValue(BSegmentProperty, value); }
        /// <summary>                                                                   
        /// C  
        /// </summary>   
        public bool CSegment { get => this.GetValue<bool>(CSegmentProperty); set => SetValue(CSegmentProperty, value); }
        /// <summary>                                                                    
        /// D                                                                            
        /// </summary>                                                                   
        public bool DSegment { get => this.GetValue<bool>(DSegmentProperty); set => SetValue(DSegmentProperty, value); }
        /// <summary>                                                                   
        /// E                                                                           
        /// </summary>                                                                  
        public bool ESegment { get => this.GetValue<bool>(ESegmentProperty); set => SetValue(ESegmentProperty, value); }
        /// <summary>                                                                   
        /// F                                                                           
        /// </summary>                                                                  
        public bool FSegment { get => this.GetValue<bool>(FSegmentProperty); set => SetValue(FSegmentProperty, value); }
        /// <summary>                                                                   
        /// G                                                                           
        /// </summary>                                                                  
        public bool GSegment { get => this.GetValue<bool>(GSegmentProperty); set => SetValue(GSegmentProperty, value); }
    }
}
